package com.zoke.support.task;

import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

import android.os.AsyncTask;

/**
 * 
 * 简单异步
 * 
 * @author JackWu 2013-8-15 支持3.0以上的api 最低支持小于3.0的app 不建议用此方法
 * 
 * 
 */
public class SimpleAsync extends AsyncTask<Void, Long, Object> {
	private SimpleAsync.Callback mCallback;

	/** 开启线程池模式 **/
	public static ExecutorService SINGLE_TASK_EXECUTOR = Executors
			.newSingleThreadExecutor();
	public static ExecutorService LIMITED_TASK_EXECUTOR = Executors
			.newFixedThreadPool(7);
	public static ExecutorService FULL_TASK_EXECUTOR = Executors
			.newCachedThreadPool();

	public SimpleAsync(SimpleAsync.Callback callback) {
		setCallback(callback);
	}

	@Override
	protected Object doInBackground(Void... voids) {
		if (mCallback != null) {
			mCallback.onStart();
		}
		return null;
	}

	@Override
	protected void onPostExecute(Object result) {
		super.onPostExecute(result);
		if (mCallback != null) {
			mCallback.onFinish();
		}
	}

	@Override
	protected void onPreExecute() {
		super.onPreExecute();
		if (mCallback != null) {
			mCallback.onPrepare();
		}
	}

	public void setCallback(SimpleAsync.Callback callback) {
		this.mCallback = callback;
	}

	public void go(ExecutorService type) {
		this.executeOnExecutor(type);
	}

	public void go() {
		this.executeOnExecutor(SINGLE_TASK_EXECUTOR);
	}

	public interface Callback {
		public void onPrepare();

		public void onStart();

		public void onFinish();
	}
}
